<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>编辑仓库</title>
    <script type="text/javascript">
      Ext.onReady(function(){
        var warehouseId = <%=params[:id]%>;
        var number = new Ext.form.NumberField({
          fieldLabel: '编号',
          allowBlank: false,
          blankText: "编号不能为空",
          name: 'number',
          anchor: '95%'
        });

        var name = new Ext.form.TextField({
          fieldLabel: '名称',
          allowBlank: false,
          blankText: "仓库名称不能为空",
          name: 'name',
          anchor: '95%'
        });

        var warehouseTypeTemplate = new Ext.XTemplate('<tpl for="."><div class="search-item">', '{name}', '</div></tpl>');
        warehouseTypeTemplate.compile();
        var warehouseTypeStore = new Ext.data.Store({
          proxy: new Ext.data.HttpProxy({
            url: "/warehouse_type/getAllWarehouseTypes?for_cbb=true"
          }),
          reader: new Ext.data.JsonReader({
            totalProperty: 'totalProperty',
            root: 'root'
          }, [{
              name: 'id'
            },{
              name: 'name'
            }])
        });

        var warehouseType = new Ext.form.ComboBox({
          fieldLabel: '类型',
          store: warehouseTypeStore,
          allowBlank: false,
          typeAhead: true,
          triggerAction: 'all',
          selectOnFocus:true,
          valueField: 'id',
          displayField: 'name',
          hiddenName: 'warehouse_type_id',
          emptyText:'选择仓库类型...',
          //allowBlank: false,
          anchor: '95%'
        });

        var desc = new Ext.form.TextArea({
          fieldLabel: '描述',
          name: 'description',
          height:80,
          anchor: '95%'
        });
        
        var editWarehouseForm = new Ext.form.FormPanel({
          //baseCls: 'x-plain',
          frame: true,
          labelWidth: 75,
          width: 290,
          height:205,
          labelAlign: 'right',
          url: '/warehouse/update?id=' + warehouseId,
          method: 'GET',
          reader: new Ext.data.JsonReader({
            root: 'root'
          }, [{
              name: 'id'
            }, {
              name: 'number'
            }, {
              name: 'name'
            }, {
              name: 'warehouse_type_id'
            }, {
              name: 'description'
            }]),
          items: [number,name,warehouseType,desc],
          buttons: [{
              text: '提交',
              iconCls: 'icon-save',
              handler: function(){
                // check form value
                if (editWarehouseForm.form.isValid()) {
                  editWarehouseForm.form.submit({
                    method: 'GET',
                    success: function(result, response){
                      if (response.result.info == "成功") {
                        //更新成功后，隐藏窗口，并刷新Grid
                        editWarehouseWin.destroy();
                      }
                      else {
                        Ext.MessageBox.alert('消息', response.result.info);
                      }
                    },
                    failure: function(result, response){
                      Ext.MessageBox.alert('提示', result.responseText);
                    }
                  });
                }
                else {
                  Ext.MessageBox.alert('错误提示', '请按要求填写必输选项.');
                }
              }
            },{
              text: '关闭',
              iconCls: 'exit',
              handler:function(){
                //此处的窗口是hide的，所以一定要reset，防止数据混乱。
                editWarehouseForm.getForm().reset();
                editWarehouseWin.destroy();
              }
            }]
        });
        //编辑前，把数据引入到表单中。
        // load form and assign value to fields
        warehouseTypeStore.load({callback: function(record, options, success){
            if(success){
              editWarehouseForm.form.load({
                url: '/warehouse/edit?id=' + warehouseId,
                method : 'GET',
                waitMsg: '正在载入数据...',
                success: function(form, action){
                },
                failure: function(form, action){
                  Ext.MessageBox.alert('提示信息', '信息加载失败');
                }
              });
            }else{
              Ext.Msg.alert('操作','失败！');
            }
          }});          
        
        editWarehouseForm.render('edit_warehouse_form');
      })
    </script>
  </head>
  <body>
    <div id="edit_warehouse_form"></div>
  </body>
</html>
